package com.yzh.pinboard.FileExplore;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.yzh.pinboard.R;
import com.yzh.pinboard.utils.DensityUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileExploreDialog extends Dialog {

    private FileExploreAdapter mFileExploreAdapter;
    private List<File> mPathTree;
    private Context mContext;
    private Button mButton;
    private OnFileChooseListener mOnFileChooseListener;

    public FileExploreDialog(Context context) {
        super(context, R.style.Translucent_NoTitle);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPathTree = new ArrayList<>();
        setContentView(R.layout.dialog_file_explore);
        initView();
    }

    @Override
    public void show() {
        super.show();
        try {
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.width = DensityUtil.getScreenWidth(mContext);
            params.height = DensityUtil.getScreenHeight(mContext) - DensityUtil.getStatusBarHeight
                    (mContext);
            getWindow().setAttributes(params);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void initView() {
        ListView listView = (ListView) findViewById(R.id.listView);
        mButton = (Button) findViewById(R.id.back);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPathTree.size() > 0) {
                    File file = mPathTree.remove(mPathTree.size() - 1);
                    mFileExploreAdapter.setData(file.listFiles());
                }
            }
        });
        File parentPath = Environment.getExternalStorageDirectory();
        File[] data = parentPath.listFiles();
        mFileExploreAdapter = new FileExploreAdapter(mContext, data);
        listView.setAdapter(mFileExploreAdapter);

        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position,
                                            long id) {

                        File file = mFileExploreAdapter.getItem(position);

                        if (file.isDirectory()) {
                            mPathTree.add(file.getParentFile());
                            mFileExploreAdapter.setData(file.listFiles());
                        } else {
                            if (!file.getName().endsWith("jpg")){
                                mOnFileChooseListener.onErr();
                                return;
                            }
                            if (mOnFileChooseListener != null) {
                                File[] files = file.getParentFile().listFiles();
                                List<String> pics = new ArrayList<String>();

                                int p = 0;
                                boolean add = true;
                                for (File f : files) {
                                    if (f.getName().endsWith("jpg")) {
                                        pics.add(f.getPath());

                                    } else {
                                        continue;
                                    }
                                    if (file.getName().equals(f.getName())) {
                                        add = false;
                                    }
                                    if (add) {
                                        p++;
                                    }
                                }
                                mOnFileChooseListener.onChoose(file.getPath(),
                                        file.getParentFile().getName(), p, pics);
                            }
                            dismiss();
                        }

                    }
                }
        );
    }

    public interface OnFileChooseListener {
        /**
         * @param groupPath 所选择的的图片路径
         * @param groupName 所在文件夹名字
         * @param position  所选择的图片在图片中的位置（已排除无用的文件类型）
         * @param imgList   图片所在的路径下所有的图片列表
         */
        void onChoose(String groupPath, String groupName, int position, List<String> imgList);
        void onErr();
    }

    public void setOnFileChooseListener(OnFileChooseListener onFileChooseListener) {
        mOnFileChooseListener = onFileChooseListener;
    }
}
